import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use("TKAgg")
plt.style.use(["science", "grid", "no-latex", "cjk-sc-font"])
plt.rcParams["font.sans-serif"] = ['KaiTi']
plt.rcParams["axes.unicode_minus"] = False
plt.rcParams["figure.figsize"] = (15.2, 8.8)
fontdict = {"fontsize": 20}


def gussian_dis(x, u, sigma):
    return 1/(np.sqrt(2*np.pi)*sigma)*np.exp(-(x-u)**2/(2*sigma**2))


r = np.array([[11575, 11469, 11491, 11541, 11493, 11539, 11745, 11423, 11434, 11638],
              [5126, 5177, 5174, 5115, 5133, 5126, 5121, 5082, 5088, 5094],
              [4863, 4928, 4973, 4985, 4946, 4951, 4976, 4951, 4960, 4961],
              [4900, 4900, 4991, 4867, 4861, 4855, 4875, 4872, 4901, 4859]]).astype(float)

r[0, :] = r[0, :] / 1000
u = r[0, :].mean()
s = r[0, :].std()
x = np.linspace(u-3*s, u+3*s, 100000)
plt.title("无优化时的运行时间")
plt.xlabel("运行时间/s")
plt.ylabel("按正态分布估计后的概率密度")
plt.scatter(r[0, :], gussian_dis(r[0, :], u, s), marker="*")
plt.plot(x, gussian_dis(x, u, s))
plt.plot([u, u], [0, gussian_dis(u, u, s)], "k--")
plt.text(u-0.01, gussian_dis(u, u, s), "平均用时:{}s".format(round(u, 3)))
plt.show()

for i in range(1, r.shape[0]):
    r[i, :] = r[i, :] / 1000
    u = r[i, :].mean()
    s = r[i, :].std()
    x = np.linspace(u-3*s, u+3*s, 100000)
    plt.title("各种优化后的运行时间对比")
    plt.xlabel("运行时间/s")
    plt.ylabel("按正态分布估计后的概率密度")
    plt.plot(x, gussian_dis(x, u, s))
    plt.scatter(r[i, :], gussian_dis(r[i, :], u, s), marker="*")
    plt.plot([u, u], [0, gussian_dis(u, u, s)], "k--")
    plt.text(u-0.01, gussian_dis(u, u, s), "平均用时:{}s".format(round(u, 3)))
plt.legend(["字节复制->双字复制", "字节复制->双字复制", "",
            "除法->移位", "除法->移位","加减法->取地址", "加减法->取地址", ""])
plt.show()
